--
-- Author: shunguo.chen
-- Date: 2016-12-09 14:08:42
--
-- SplusController

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

local DCAgent = import("..Splus.DCAgent")
local DCAccount = import("..Splus.DCAccount")
local DCRole = import("..Splus.DCRole")
local DCLevels = import("..Splus.DCLevels")
local DCCoin = import("..Splus.DCCoin")
local DCItem = import("..Splus.DCItem")
local DCTask = import("..Splus.DCTask")
local DCNewbieTask = import("..Splus.DCNewbieTask")

function ClassRef:ctor(params)
	self.super.ctor(self, params)

end

function ClassRef:init()
	local eventNames = {"OnChannelLogined", "OnGameLogined", "OnRoleCreated", "OnGameConnected"}
	self:addListeners(eventNames, handler(self, self.onLoginEvent))

	self:addListeners({"onGuideFinished"}, handler(self, self.onNewbieEvent))

	self._playerHandle = UD.player:onChanged(handler(self, self.updatePlayer))
	self._walletHandler = UD:onChanged("wallet", handler(self, self.updatePlayer))
	self._bagHandle = UD.bag:onChanged(handler(self, self.updateBag))
end

function ClassRef:removeListeners()
	self.super.removeListeners(self)
	UD.player:unChanged(self._playerHandle)
	UD.bag:unChanged(self._bagHandle)
	UD:unChanged("wallet", self._walletHandler)
end

function ClassRef:onLoginEvent(eventName, data)
	print("SplusController.onLoginEvent eventName =", eventName)
	if eventName == "OnChannelLogined" then

	elseif eventName == "OnGameLogined" then
		self._account = data.account
		DCAccount.login(self._account, "-", "null", 0)
	elseif eventName == "OnRoleCreated" or eventName == "OnGameConnected" then
		local player = UD.player
		self._uid = player.uid
		local name = player.name
		local sex = player.sex
		local level = player.level
		local job = player:getClassName()
		local zone = UD.zoneName
		if eventName == "OnRoleCreated" then
			DCRole.createRole(self._account, uid, name, job, sex, level, zone)
		else
			DCRole.enable(uid, name, job, sex, level, zone)
		end
	elseif eventName == "OnRoleDisable" then
		DCRole.enable()
	end
end

function ClassRef:onNewbieEvent(eventName, data)
	if eventName == "onGuideFinished" then
		DCNewbieTask.setStep(L("lua_code_text_95"))
	end
end

function ClassRef:updatePlayer(name, old, new)
	if name == "level" then
		print("SplusController.levelUp")
		DCRole.levelUp(self._uid, old, new, 0)
	elseif name == "coin" or name == "gold" then
		local coinType
		if name == "coin" then
			coinType = L("lua_code_text_457")
		elseif name == "gold" then
			coinType = L("lua_code_text_195")
		end
		print("SplusController.DCCoin", coinType)
		if old > new then
			DCCoin.use("－", coinType, old - new, UD.player.level)
		else
			DCCoin.get("－", coinType, new - old, UD.player.level)
		end
	end
end

function ClassRef:updateBag(uid,item,amountGot)
	local itemId = item.tid
	local itemType = L("lua_code_text_240")
	if item.itemBagType == Enums.ItemBagType.equip then
		itemType = L("lua_code_text_263")
	elseif item.itemBagType == Enums.ItemBagType.item then
		itemType = L("lua_code_text_125")
	end
	print("SplusController.DCItem", itemType)
	if amountGot > 0 then
		DCItem.get(itemId, itemType, amountGot, 0, "-", "-", UD.player.level)
	else
		DCItem.use(itemId, itemType, -amountGot, "-", UD.player.level)
	end
end

return ClassRef
